package com.lhd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lhd.mapper.RecordMapper;
import com.lhd.pojo.PageBean;
import com.lhd.pojo.PageCondition;
import com.lhd.pojo.Record;
import com.lhd.service.RecordService;
import com.lhd.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

@Service
public class RecordServiceImpl implements RecordService {
    @Autowired
    RecordMapper recordMapper;

    @Override
    public void add(Record record) {
        Integer id = getId();
        record.setUid(id);
        recordMapper.add(record);
    }

    @Override
    public List<Record> getByUid() {
        Integer id = getId();
        return recordMapper.getByUid(id);
    }

    @Override
    public List<Record> getAll() {
        return recordMapper.getAll();
    }

    @Override
    public PageBean<Record> getByCondition(String uname, String gname, String sname, PageCondition pageCondition) {
        PageHelper.startPage(pageCondition.getPageNum(),pageCondition.getPageSize());
        Page<Record> page = recordMapper.getByCondition(uname,gname,sname);
        long totals = page.getTotal();
        List<Record> result = page.getResult();
        return new PageBean<>(totals,result);
    }

    @Override
    public void updateState(Integer state, Integer id) {
        recordMapper.updateState(state,id);
    }


    private Integer getId() {
        Map<String, Object> map = ThreadLocalUtil.get();
        return (Integer) map.get("id");
    }
}
